package tgc.rj.zz.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tgc.rj.zz.custom.DatagridFormUtils;
import tgc.rj.zz.custom.LogUtils;
import tgc.rj.zz.entity.Books;
import tgc.rj.zz.entity.SysUser;
import tgc.rj.zz.security.UserUtils;
import tgc.rj.zz.service.BooksService;

@Controller
@RequestMapping(value="books")
public class BookController {
	@Autowired
	private BooksService service;
	@Autowired
	private LogUtils logUtils;
	@Autowired
	private  UserUtils userUtils;
	private String path="e:/files/libary";
	
/*	@RequestMapping("/book")
	public String book(){
		return "books/book";
	}
	
	@RequestMapping(value="listBooks")
	@ResponseBody
	public Object listBooks(DatagridFormUtils custom){
		Pageable pageable = custom.buildPageable();
		Page<Books> page = service.findAll(pageable);
		HashMap<String, Object> result = DatagridFormUtils.getPageResult(page);
		return result;
	}
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		service.download(id, request, response);
		return null;
	}
	@RequestMapping(value="/save")
	@ResponseBody
	public String save(MultipartFile file,Books book) {
		service.save(file,book);
		SysUser user = userUtils.getUser();
		logUtils.log(user.getUsername(),user.getName(),"上传图书");
		return "ok";
	}*/
	
	/*
	 * 
	
	 * 所有图书页面
	 */
	@RequestMapping(value="bookAll")
	public String bookAll(ModelMap map) {
		List<Books> books = service.findAll();
		map.put("books", books);
		map.put("path", path);
		return "books/bookAll";
	}
	
	/*
	 * 图书新增页面
	*/
	 
	@RequestMapping(value="/edit")
	public String edit() {
		return "books/bookEdit";
	}
	
	
	 /*
	  * 上传图书
	  */
	 
	@RequestMapping(value="/save")
	public String save(MultipartFile file,Books book) {
		service.save(file,book);
		SysUser user = userUtils.getUser();
		logUtils.log(user.getUsername(),user.getName(),"上传图书");
		return "redirect:bookAll";
	}
	
	 /*
	  * 下载图书图片
	  */
	 
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		service.download(id, request, response);
		return null;
	}
	
	 /*
	  * 删除图书
	  */
	 
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(Integer id) {
		service.deleteById(id);
		//Books book = service.findById(id);
		//String title = book.getTitle();
		SysUser user = userUtils.getUser();
		logUtils.log(user.getUsername(),user.getName(),"删除图书");
		return "删除成功";
	}
	
	 /*
	  * 查询图书
	  */
	 
	@RequestMapping(value="searchbtnBook")
	public String searchbtnBook(String title,ModelMap map) {
		List<Books> books= service.findByTitleLike("%"+title+"%");
		map.put("books", books);
		return "books/searchBook";
		
	}

}
